import { ValidateRule } from "../utils/validate";
import { Card, CardRight } from '../model/card';

export interface IPutCardForm extends Card {
  rights: CardRight[];
}

/**
 * 【校验规则】发布会员卡
 */
export const RuleCardPut: ValidateRule<IPutCardForm> = {
  name: [
    { required: true, message: '请填写权益卡名称' },
    { max: 9, message: '权益卡名称不得超过9个字' },
  ],
  background_pic_url: {
    required: true,
    message: '请上传卡片背景图片',
    enable: values => values.color_type === 2
  },
  description: [
    {
      required: true,
      message: '请填写使用须知',
    },
    {
      max: 800,
      message: '使用须知不得超出800个字符',
    },
  ],
  term_start_time: {
    required: true,
    message: '请选择有效期开始时间',
    enable: values => values.term_type === 2,
  },
  term_end_time: [
    {
      required: true,
      message: '请选择有效期结束时间',
      enable: values => values.term_type === 2,
    },
    {
      enable: values => values.term_type === 2 && !!values.term_start_time,
      validator: (rule, value, cb, source) => {
        if (new Date(source.term_start_time || '') > new Date(value || '')) {
          return cb('结束时间不可小于开始时间');
        }
        cb();
      },
    },
  ],
  term_days: [
    {
      enable: values => values.term_type === 3,
      validator: (rule, value, cb) =>
        cb(typeof value !== 'number' || value < 1 ? '有效期天数最小为1天' : undefined),
    },
  ],
  price: [
    {
      enable: values => values.type === 3,
      type: 'price',
      allowEmpty: false,
      message: '请输入正确的金额',
    },
    {
      enable: values => values.type === 3,
      validator: (rule, value, cb) =>
        cb(!value || value == 0 ? '购买价格不得为0' : undefined),
    },
  ],
  quantity: {
    enable: values => values.type === 3,
    type: 'number',
    message: '请输入正确的库存数字',
  },
  rule_condition: {
    enable: values => values.type === 2,
    validator: (rule, value, cb, source) => {
      let { rule_condition } = source;
      if (rule_condition) {
        let { amount_limit, points_limit, trade_limit } = rule_condition;
        if (!amount_limit && !points_limit && !trade_limit) return cb('请至少填写一个条件');
        cb();
      }
    },
  },
};

/**
 * [API] 获取会员卡数据
 */
export interface ServiceCardGetCardParams {
  // 主键ID 如果传入ID则只返回一个项目
  card_id?: number;
  currentPage?: number;
  pageSize?: number;
  /**
   * 是否返回总量统计
   */
  count?: boolean;
  filterForm?: {
  };
}
